// $codepro.audit.disable packagePrefixNamingConvention, spellCheckComments, spellCheckIdentifiers, spellCheckStringLiterals
/**
 * Package domain
 */
package teamrocket.domain;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;

import teamrocket.db.DBWrapper;
import teamrocket.logging.TLogger;

/**
 * Represents a treatment record in the db
 * @author Stephen
 * @version 1.0
 */
public class TreatmentRecord implements IDBObject<TreatmentRecord>{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Field doctorsOrders.
	 */
	private int doctorsOrders;
	
	/**
	 * Field vitalSigns.
	 */
	private VitalSigns vitalSigns;
	
	/**
	 * Field doctorid.
	 */
	private int doctorid;
	
	/**
	 * Field nurseid.
	 */
	private int nurseid;
	
	/**
	 * Field patientid.
	 */
	private int patientid;
	
	/**
	 * Field date.
	 */
	private Date date;
	
	/**
	 * Field diagnosis.
	 */
	private String diagnosis;
	
	/**
	 * Field symptoms.
	 */
	private String symptoms;
	
	/**
	 * Field recordid.
	 */
	private int recordid;
	
	/**
	 * Constructs a treatment record in memory
	 * @param doctorsOrders
	 * @param vitalSigns
	 * @param doctor
	 * @param nurse
	 * @param patient
	 * @param date
	 * @param diagnosis
	 * @param symptoms
	 */
	public TreatmentRecord(int doctorsOrders, VitalSigns vitalSigns, // $codepro.audit.disable largeNumberOfParameters
			int doctor, int nurse, int patient, Date date, String diagnosis,
			String symptoms) {
		this.doctorsOrders = doctorsOrders;
		this.vitalSigns = vitalSigns;
		this.doctorid = doctor;
		this.nurseid = nurse;
		this.patientid = patient;
		this.date = date;
		this.diagnosis = diagnosis;
		this.symptoms = symptoms;
		TLogger.getInstance().getLogger().log(Level.INFO,
				"Creating Treatment Record for Patient " + patientid);
	}
	
	/**
	 * Method getDoctorsOrders.
	 * @return int
	 */
	public int getDoctorsOrders() {
		return doctorsOrders;
	}
	
	/**
	 * Method setDoctorsOrders.
	 * @param doctorsOrders int
	 */
	public void setDoctorsOrders(int doctorsOrders) {
		this.doctorsOrders = doctorsOrders;
	}
	
	/**
	 * Method getVitalSigns.
	 * @return VitalSigns
	 */
	public VitalSigns getVitalSigns() {
		return vitalSigns;
	}
	
	/**
	 * Method setVitalSigns.
	 * @param vitalSigns VitalSigns
	 */
	public void setVitalSigns(VitalSigns vitalSigns) {
		this.vitalSigns = vitalSigns;
	}
	
	/**
	 * Method getPatientid.
	 * @return int
	 */
	public int getPatientid() {
		return patientid;
	}
	
	/**
	 * Method setPatientid.
	 * @param patientid int
	 */
	public void setPatientid(int patientid) {
		this.patientid = patientid;
	}
	
	/**
	 * Method getDoctor.
	 * @return int
	 */
	public int getDoctor() {
		return doctorid;
	}
	
	/**
	 * Method setDoctor.
	 * @param doctor int
	 */
	public void setDoctor(int doctor) {
		this.doctorid = doctor;
	}
	
	/**
	 * Method getNurse.
	 * @return int
	 */
	public int getNurse() {
		return nurseid;
	}
	
	/**
	 * Method setNurse.
	 * @param nurse int
	 */
	public void setNurse(int nurse) {
		this.nurseid = nurse;
	}
	
	/**
	 * Method getDate.
	 * @return Date
	 */
	public Date getDate() {
		return date;
	}
	
	/**
	 * Method setDate.
	 * @param date Date
	 */
	public void setDate(Date date) {
		this.date = date;
	}
	
	/**
	 * Method getDiagnosis.
	 * @return String
	 */
	public String getDiagnosis() {
		return diagnosis;
	}
	
	/**
	 * Method setDiagnosis.
	 * @param diagnosis String
	 */
	public void setDiagnosis(String diagnosis) {
		this.diagnosis = diagnosis;
	}
	
	/**
	 * Method getSymptoms.
	 * @return String
	 */
	public String getSymptoms() {
		return symptoms;
	}
	
	/**
	 * Method setSymptoms.
	 * @param symptoms String
	 */
	public void setSymptoms(String symptoms) {
		this.symptoms = symptoms;
	}
	
	/**
	 * Method getRecordID.
	 * @return int
	 */
	public int getRecordID(){
		return this.recordid;
	}
	
	/**
	 * Method setRecordID.
	 * @param recordid int
	 */
	public void setRecordID(int recordid){
		this.recordid = recordid;
	}
	
	/**
	 * Method create.
	 * @return IDBObject<TreatmentRecord>
	 * @see teamrocket.domain.IDBObject#create()
	 */
	@Override
	public IDBObject<TreatmentRecord> create() {
		try {
			try {
				DBWrapper.execute("INSERT INTO treatmentrecords " +
						"(date, doctorid, nurseid, patientid, drsorders, " +
						"diagnosis, symptoms, bloodpressure, " +
						"pulse, temperature, height, weight) " +
						" VALUES (?, ?,?,?,?,?,?,?,?,?,?,?)",
						date.getTime(),
						doctorid, 
						nurseid,
						patientid,
						doctorsOrders,
						diagnosis,
						symptoms,
						vitalSigns.getBloodPressure(),
						vitalSigns.getPulse(),
						vitalSigns.getTemp(),
						vitalSigns.getHeight(),
						vitalSigns.getWeight()
						
						);
				
				if(DBWrapper.DEBUG){
					Thread.sleep(1000); // $codepro.audit.disable numericLiterals
				}
				
				final ResultSet result = DBWrapper.query("SELECT id " +
						"FROM treatmentrecords WHERE patientid=? AND drsorders=?", 
						this.patientid, this.doctorsOrders);
				
				boolean empty = true;
				while(result.next()){ // $codepro.audit.disable nullPointerDereference
					empty = false;
					this.recordid = result.getInt("id");
				}
				if(empty){
					throw new Exception("Unable to retrieve Treatment Record ID.");
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		TLogger.getInstance().getLogger().log(Level.INFO,
				"Creating Treatment Record for Patient " +
				patientid + " into database.");
		return this;
	}
	
	/**
	 * Method update.
	 * @see teamrocket.domain.IDBObject#update()
	 */
	@Override
	public void update(){
		try {
			DBWrapper.execute("UPDATE treatmentrecords SET " +
					"date = ?, " +
					"doctorid = ?, " +
					"nurseid = ?, " +
					"patientid = ?, " +
					"drsorders = ?, " +
					"diagnosis = ?, " +
					"symptoms = ?, " +
					"bloodpressure = ?, " +
					"pulse = ?, " +
					"temperature = ?, " +
					"height = ?, " +
					"weight = ? WHERE id= ?",
					date.getTime(),
					doctorid, 
					nurseid,
					patientid,
					doctorsOrders,
					diagnosis,
					symptoms,
					vitalSigns.getBloodPressure(),
					vitalSigns.getPulse(),
					vitalSigns.getTemp(),
					vitalSigns.getHeight(),
					vitalSigns.getWeight(),
					this.recordid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		TLogger.getInstance().getLogger().log(Level.INFO,
				"Updating Treatment Record for Patient " +
				patientid + " in database.");
	}
	
	/**
	 * Method destroy.
	 * @see teamrocket.domain.IDBObject#destroy()
	 */
	@Override
	public void destroy(){
		try {
			DBWrapper.execute("DELETE FROM treatmentrecords WHERE id = ?", 
					this.recordid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		TLogger.getInstance().getLogger().log(Level.INFO,
				"Deleting Treatment Record for Patient " +
				patientid + " in database.");
	}
	
	/**
	 * Gets all of the treatment records for a certain patient
	 * @param patientid
	
	 * @return List<TreatmentRecord>
	 */
	public static List<TreatmentRecord> getTreatmentRecords(int patientid){
		final List<TreatmentRecord> records = new ArrayList<TreatmentRecord>();
		
		try {
			final ResultSet rs = DBWrapper.query("SELECT * FROM " +
					"treatmentrecords WHERE patientid = ?", patientid);
			
			while(rs.next()){
			VitalSigns vitals = new VitalSigns(
					rs.getInt("bloodpressure"),
					rs.getInt("pulse"),
					rs.getFloat("temperature"),
					rs.getInt("height"),
					rs.getFloat("weight"));
			
			TreatmentRecord record = new TreatmentRecord(
					rs.getInt("drsorders"),
					vitals,
					rs.getInt("doctorid"),
					rs.getInt("nurseid"),
					rs.getInt("patientid"),
					new Date(rs.getLong("date")),
					rs.getString("diagnosis"),
					rs.getString("symptoms"));
			
				record.setRecordID(rs.getInt("id"));
			
				records.add(record);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		TLogger.getInstance().getLogger().log(Level.INFO,
				"Checking all Treatment Records for Patient.");
		return records;
	}

	/**
	 * Method getDoctorName.
	 * @return String
	 */
	public String getDoctorName() {
		//Patient patient = Patient.loadPatientFromId(patientid);
		try {
			final User doctor =  User.createFilledUser(getDoctor());
			if(doctor == null){
				return "";
			}
			return doctor.getFirstName() + " " + doctor.getLastName();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "";

	}
	
	/**
	 * Method getNurseName.
	 * @return String
	 */
	public String getNurseName(){
		try {
			final User nurse =  User.createFilledUser(getNurse());
			if(nurse == null){
				return "";
			}
			return nurse.getFirstName() + " " + nurse.getLastName();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * Method getPatient.
	 * @return Patient
	 */
	public Patient getPatient(){
		return Patient.loadPatientFromId(patientid);
	}
	
	/**
	 * Method getDoctorsOrders.
	 * @param orderid int
	 * @return DoctorsOrders
	 */
	public DoctorsOrders getDoctorsOrders(int orderid){
		final DoctorsOrders[] drsorders = new DoctorsOrders[1];
		final Collection<DoctorsOrders> drorderscoll =
			DoctorsOrders.getDrsOrders(orderid);
		if(drorderscoll.size() > 0){
			return drorderscoll.toArray(drsorders)[0];
		}
		else{
			return null;
		}
	}
	
	/**
	 * Method toString.
	 * @return String
	 */
	@Override
	public String toString(){
		final SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
		return sdf.format(getDate());
		
	}
}